// import FastClick from "fastclick";

/**
 * 初始化页面
 * @param d  document
 * @param w  window
 */

export const viewInit = (d, w) => {
  // 设置webview初始样式
  setWebviewStyle();

  // 设置根字体大小
  // setHtmlFontSize();

  // 消除click时间延迟
  // FastClick.attach(d.body);

  // //处理iOS11及以上点击延迟问题
  // const device = navigator.userAgent
  //   .toLowerCase()
  //   .match(/cpu iphone os (.*?) like mac os/);
  // if (!device || parseInt(device[1]) < 11) {
  //   FastClick.attach(document.body);
  // }

  //取消浏览器的所有事件，使得active的样式在手机上正常生效
  d.addEventListener("touchstart", () => false, true);

  // 屏蔽选择函数
  d.oncontextmenu = () => false;
};

/**
 * 设置webview初始样式
 */

export const wvStyle = () => {
  const w = window;
  const styles = {
    scrollIndicator: "none",
    scalable: false,
    popGesture: "none",
    plusrequire: "ahead",
    kernel: "WKWebview"
  };
  if (w.plus.os.name === "iOS" && parseInt(w.plus.os.version) < 8) {
    styles.kernel = "UIWebview";
  }
  if (
    !w.plus.webview.defaultHardwareAccelerated() &&
    parseInt(w.plus.os.version) >= 5
  ) {
    styles.hardwareAccelerated = true;
  }
  return styles;
};

export const setWebviewStyle = () => {
  const wv = window.plus.webview.currentWebview();
  wv.setStyle(wvStyle());
};

/**
 * js设置元素px时进行dpr缩放
 * webkit内核需要成dpr得到px
 * @param num  传入逻辑像素值
 */

export const getPX = num => {
  const wv = window.plus.webview.currentWebview();
  if (wv.getStyle().kernel === "UIWebview") {
    return num;
  }
  const dpr = window.devicePixelRatio;
  if (dpr > 1) {
    return num * dpr;
  } else {
    return num;
  }
};

/**
 * 获取通知栏高度
 */

/* export const getImmersed = () => {
    return getPX(window.plus.navigator.getStatusbarHeight());
} */

/**
 * 设置根字体大小
 */

export const setHtmlFontSize = () => {
  document.getElementsByTagName("html")[0].style.fontSize = `${getPX(10)}px`;
};

/**
 * 打开页面
 */

let _openw = null;
let preate = {};

/**
 * 点击打开新窗口
 * @param id	加载的页面地址，也用作窗口标识
 * @param a	页面动画，默认使用'pop-in'
 * @param s	是否不显示窗口
 */

export const linkTo = (id, a, s) => {
  if (_openw) return;

  a || (a = "pop-in");
  _openw = preate[id];

  if (_openw) {
    _openw.showded = true;
    _openw.show(a, null, () => {
      _openw = null; // 避免快速点击打开多个页面
    });
  } else {
    _openw = window.plus.webview.create(id, id);
    preate[id] = _openw;

    _openw.addEventListener(
      "loaded",
      () => {
        // 页面加载完成后才显示
        setTimeout(() => {
          _openw.showded = true;
          s ||
            _openw.show(a, null, () => {
              _openw = null; //避免快速点击打开多个页面
            });
          s && (_openw = null); //避免s模式下变量无法重置
        }, 10);
      },
      false
    );

    _openw.addEventListener(
      "hide",
      () => {
        _openw && (_openw.showded = true);
        _openw = null;
      },
      false
    );

    _openw.addEventListener(
      "close",
      () => {
        // 页面关闭后可再次打开
        _openw = null;
        preate[id] && (preate[id] = null); // 兼容窗口的关闭
      },
      false
    );
  }
};

/**
 * 返回键
 */
let backState = false;

export const back = ({ route, _this }) => {
  if (
    route.path === "/login" ||
    route.path === "/register" ||
    route.path === "/forgetPwd" ||
    route.path === "/userContract" ||
    route.path === "/performance/homePage" ||
    route.path === "/performance/msgCenter" ||
    route.path === "/performance/orderCenter" ||
    route.path === "/performance/personalCenter" ||
    route.path === "/designer/homePage" ||
    route.path === "/designer/orderCenter" ||
    route.path === "/designer/msgCenter" ||
    route.path === "/designer/personalCenter" ||
    route.path === "/touristLogin"
  ) {
    quit();
  } else if (route.path === "/performance/myIntroduction") {
    _this.$router.push("/performance/personalCenter");
  } else if (
    route.path === "/performance/skillTages" ||
    route.path === "/performance/goodatSpace" ||
    route.path === "/performance/goodatStyle" ||
    route.path === "/performance/regionYear" ||
    route.path === "/performance/newExperiences" ||
    route.path === "/performance/myExperiences"
  ) {
    _this.$router.push("/performance/myIntroduction");
  } else if (route.path === "/performance/personalEditing") {
    // 个人资料编辑  跳转 - 个人资料
    _this.$router.push("/performance/myPersonality");
  } else if (route.path === "/designer/personalEditing") {
    _this.$router.push("/designer/myPersonality");
  } else if (route.path === "/performance/myPersonality") {
    // 个人资料页面  跳转 -- 我的首页
    _this.$router.push("/performance/personalCenter");
  } else if (
    route.path === "/designer/applySuccess" ||
    route.path === "/designer/repeatApply"
  ) {
    _this.$router.push("/designer/personalCenter");
  } else if (route.path === "/designer/myInfo") {
    //设置跳回个人中心
    _this.$router.push("/designer/personalCenter");
  } else if (route.path === "/designer/newerGuide") {
    // 已经改正  新手指南 跳回  -- 设置
    _this.$router.push("/designer/myInfo");
  } else if (route.path === "/performance/myInfo") {
    // 跳转回去设置 - 我的首页
    _this.$router.push("/performance/personalCenter");
  } else if (route.path === "/performance/newerGuide") {
    // 新手指南跳回   -- 设置
    _this.$router.push("/performance/myInfo");
  } else if (route.path === "/designer/myPersonality") {
    _this.$router.push("/designer/personalCenter");
  } else if (route.path === "/performance/pay-info") {
    _this.$router.push("/");
  } else if (route.path === "/performance/bindAccount") {
    _this.$router.push("/performance/withdrawMoney");
  } else if (route.path === "/performance/withdrawMoney") {
    _this.$router.push("/performance/myWallet");
  } else if (route.path === "/performance/myWallet") {
    _this.$router.push("/performance/personalCenter");
  } else if (route.path === "/designer/applyInvoices") {
    _this.$router.push("/designer/personalCenter");
  } else if (route.path === "/designer/eachInvoice") {
    _this.$router.push("/designer/applyInvoices");
  } else if (route.path === "/designer/addressList") {
    _this.$router.push("/designer/eachInvoice");
  } else if (route.path === "/touristLogin") {
    _this.$router.push("/login");
  } else {
    if (!backState) {
      backState = true;
      setTimeout(() => {
        history.back();
        backState = false;
      }, 100);
    }
  }
};

let ws = null;
/**
 * 子页面返回
 * @param hide 是否隐藏
 */

export const wvBack = hide => {
  const w = window;
  if (w.plus) {
    ws || (ws = w.plus.webview.currentWebview());
    if (hide || ws.preate) {
      ws.hide("auto");
    } else {
      ws.close("auto");
    }
  } else if (history.length > 1) {
    history.back();
  } else {
    w.close();
  }
};
/**
 * 退出app
 */

export const quit = () => {
  const w = window;
  w.plus.nativeUI.confirm(
    "确定要退出图小助吗？",
    function(e) {
      if (e.index == 0) {
        w.plus.nativeUI.toast("感谢继续使用图小助", {
          verticalAlign: "center"
        });
      } else if (e.index == 1) {
        w.plus.runtime.quit();
      }
    },
    { title: "图小助", buttons: ["否", "是"], verticalAlign: "center" }
  );
};

/**
 * 网络监听
 */

export const networkState = () => {
  const w = window;
  const network = w.plus.networkinfo.getCurrentType();
  let result;
  switch (network) {
    case w.plus.networkinfo.CONNECTION_ETHERNET:
    case w.plus.networkinfo.CONNECTION_WIFI:
      result = 2;
      break;
    case w.plus.networkinfo.CONNECTION_CELL2G:
    case w.plus.networkinfo.CONNECTION_CELL3G:
    case w.plus.networkinfo.CONNECTION_CELL4G:
      result = 1;
      break;
    default:
      result = 0;
      break;
  }
  return result;
};
